﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Data;
using System.Collections.Specialized;
using System.Collections;
using System.Data;
namespace iVision
{
    public class ListViewFilter
    {
       
        private StringDictionary lvFilterDic = new StringDictionary();
        private Dictionary<ListView, StringDictionary> ListViewDic = new Dictionary<ListView, StringDictionary>();

        public ListViewFilter(List<ListView> ListViewArray)
        {
            foreach (ListView listView in ListViewArray)
            {
                StringDictionary lvFilter = new StringDictionary();
                ListViewDic.Add(listView, lvFilter);
            }
        }

        public void addFilterToListViewFilterList(ListView lv, String fieldName, String filterValue)
        {

            StringDictionary filter = getListViewFilterList(lv);
            if (filter.Equals(null)) return;
            KeyValuePair<String, String> kvpToInsert = new KeyValuePair<String, String>(fieldName, filterValue);
            if (filter.ContainsKey(fieldName)) filter[fieldName] = filterValue;
            else filter.Add(fieldName, filterValue);

        }

        public void filterListView(ListView lv)
        {
            String filterStatement = String.Empty;
            StringDictionary filter = getListViewFilterList(lv);
            if (filter.Equals(null)) return;

            GridView gv = (GridView)lv.View;
            //Get view from listview itemsource
            BindingListCollectionView view = CollectionViewSource.GetDefaultView(lv.ItemsSource)
             as BindingListCollectionView;

            //Get Datatable from view.
           DataView dv = view.SourceCollection as DataView;

           DataTable dt = dv.Table;

            //Create filter String
            foreach (DictionaryEntry kvp in filter)
            {
                if ((kvp.Value as String != String.Empty) && (kvp.Value != null))
                {

                    String addStatement = generateSingleFilterStatement(dt, kvp.Key.ToString(),kvp.Value.ToString());

                    if (filterStatement == String.Empty) filterStatement += addStatement;
                    else filterStatement += " AND " + addStatement;
                }
            }
          


            if (view != null)
            {
                view.CustomFilter = filterStatement;
            }

        }

        StringDictionary getListViewFilterList(ListView lv)
        {
            return ListViewDic[lv];
        }


        public String generateSingleFilterStatement(DataTable DataTable, String Key,String Value)
        {
            Type columnDataType = DataTable.Columns[Key].DataType;
            Value = Value.Replace("\'","\'\'");
            switch (columnDataType.Name)
            {
                case "Int32": return Key + " = " + Value + "";
                case "String": return Key + " like '" + Value + "%'";
                case "Bit": return Key + " = " + Value + "";
                case "Float": return Key + " = " + Value + "";
                default: return String.Empty;
            }
        }
    }
}
